<?php
/**
 * @brief Epay95支付插件类
 * @class Epay95
 * @package App
 * @subpackage  Driver.Payment
 * @author cjli
 */
class Epay95 extends Payment implements Payment_Interface
{
	public function __construct()
	{
		$this->_paymentInfo = PaymentModel::get_payment_info('Epay95');
	}
	
  public function getSettingFields()
  {
    return array(
      'MerNo' => '商户帐号',
      'MD5key' => '商户密钥',
      'currency' => array(
        'name' => '支付币种',
        'type' => 'select',
        'options' => array(
          '1' => '美元',
          '2' => '欧元',
          '4' => '英镑',
    			'6' => '日元'
        ),
        'default' => '1',
      ),
//      'type' => array(
//        'name' => '支付类型',
//        'type' => 'select',
//        'options' => array(
//          '1' => '直接支付',
//          '2' => '跳转',
//        ),
//        'default' => '2',
//      ),
//      
//     'in_submit_url' => '跳转支付地址',
    );
  }

  public function getSubmitForm($orderInfo)
  {
  	global $language_id;
  	
  	$lang_id = isset($orderInfo['language_id']) ? $orderInfo['language_id'] : $language_id;
  	
    $dateStr = date('Ymd', time());
   	$DisAmount = $Amount = $orderInfo['pay_amount'];
    
 	 	$c = SiteModel::get_currency_info($_SESSION['currency']);
		if (isset($c['exchange']) && $c['exchange']) {
		   $DisAmount = $c['exchange'] * $orderAmount; //外币金额
		 }
		 
    $MD5key = $this->_paymentInfo['config']['MD5key'];                             //MD5私钥
    $MerNo = $this->_paymentInfo['config']['MerNo'];						                   //商户号
     $BillNo = $orderInfo['order_sn'];
		 $Currency = $this->_paymentInfo['config']['currency'];                         //币种
     
		 $DispAmount= 0;                                           //外币金额

      $languageInfo = SiteModel::get_language_info($lang_id);
		 if ($languageInfo['code'] = "fr") {
			 $Language = '3';
		 }
		 else {
			 $Language = '2';
		 }

        $ReturnURL = url('payment/callback', array('pay' => 'Epay95'));
		 		$Noticeurl= url('payment/notice', array('pay' => 'Epay95'));
		 		
		 		 $MerWebsite = getHost();//当前域名

		 		$Remark = '95epay'; //备注
         $md5src = $MerNo.$BillNo.$Currency.$Amount.$Language.$ReturnURL.$MD5key; //校验源字符串
         $MD5info = strtoupper(md5($md5src));                                   //MD5检验结果

			$shippingAddress = $orderInfo['delivery_address'];
			
			$shippingAddress .= $orderInfo['delivery_address2'] ? '&nbsp;&nbsp;'.$orderInfo['delivery_address2'] : null;
			$shippingAddress .= $orderInfo['delivery_address3'] ? '&nbsp;&nbsp;'.$orderInfo['delivery_address3'] : null;

			$shippingCity = $orderInfo['delivery_city'];
			$shippingState = $orderInfo['delivery_province'];
			
			//物品信息
			$products = '';
//			$goods_list =OrderModel::get_order_product_list($orderInfo['id']);
//			foreach ($goods_list AS $key => $goods){
//			//    $prouductsinfo=$prouductsinfo.$goods['goods_number'].'*'.$goods['goods_name'].'*'.$goods['goods_price'].'|';
//				$prouductsinfo .= $goods['qty'].'*'.$goods['product_sn'].'|';
//			}
//			$prouductsinfo = substr($prouductsinfo,0,-1);
			
			//$button = '<form action="https://www.95epay.com/payRequestAction.action" method="post" target="_blank">'.
			$button = '<form id="frm" action="http://www.dudufr.com/epay95_from.php" method="post" target="_blank">'.
                    "  <input type='hidden' name='MerNo' value='". $MerNo ."'>".
                    "  <input type='hidden' name='Currency' value='". $Currency ."'>".
                    "  <input type='hidden' name='BillNo' value='". $BillNo ."'>".
                    "  <input type='hidden' name='Amount' value='". $Amount ."'>".
                    "  <input type='hidden' name='DisAmount' value='". $DisAmount ."'>".
                    "  <input type='hidden' name='ReturnURL' value='". $ReturnURL ."'>".
										"  <input type='hidden' name='Noticeurl' value='". $Noticeurl ."'>".
                    "  <input type='hidden' name='Language' value='". $Language ."'>".
                    "  <input type='hidden' name='MD5info' value='". $MD5info ."'>".
										"  <input type='hidden' name='MerWebsite' value='". $MerWebsite ."'>".
                    "  <input type='hidden' name='Remark' value='". $Remark ."'>".
										"  <input type='hidden' name='FirstName' value='". $orderInfo['delivery_first_name'] ."'>".
										"  <input type='hidden' name='LastName' value='". $orderInfo['delivery_last_name'] ."'>".
										"  <input type='hidden' name='Email' value='". $orderInfo['delivery_email'] ."'>".
										"  <input type='hidden' name='Phone' value='". $orderInfo['delivery_phone'] ."'>".
										"  <input type='hidden' name='ZipCode' value='". $orderInfo['delivery_postcode'] ."'>".
										"  <input type='hidden' name='Address' value='". $shippingAddress ."'>".
										"  <input type='hidden' name='City' value='". $orderInfo['delivery_city'] ."'>".
										"  <input type='hidden' name='State' value='". $orderInfo['delivery_province'] ."'>".
										"  <input type='hidden' name='Country' value='". $orderInfo['delivery_country'] ."'>".
										"  <input type='hidden' name='DeliveryFirstName' value='". $orderInfo['delivery_first_name'] ."'>".
										"  <input type='hidden' name='DeliveryLastName' value='". $orderInfo['delivery_last_name'] ."'>".
										"  <input type='hidden' name='DeliveryEmail' value='". $orderInfo['delivery_email'] ."'>".
										"  <input type='hidden' name='DeliveryPhone' value='". $orderInfo['delivery_phone'] ."'>".
										"  <input type='hidden' name='DeliveryZipCode' value='". $orderInfo['delivery_postcode'] ."'>".
										"  <input type='hidden' name='DeliveryAddress' value='". $shippingAddress ."'>".
										"  <input type='hidden' name='DeliveryCity' value='". $orderInfo['delivery_city'] ."'>".
										"  <input type='hidden' name='DeliveryState' value='". $orderInfo['delivery_province'] ."'>".
										"  <input type='hidden' name='DeliveryCountry' value='". $orderInfo['delivery_country'] ."'>".
										"  <input type='hidden' name='Products' value='". $products ."'>".
										"  <input type='hidden' name='sitename' value='www.google.fr'>".
                    "  <input class='small_pay_button' type='submit' name='b1' value='" . L('_PAY_NOW_') . "'>".
                    "</form>";

        return $button;
  }
  
	public function callback()
  {
    //var_dump($_POST);
	
  if(isset($_POST) && isset($_GET)){
		extract($_POST);
		extract($_GET);
	}
	else{
		extract($HTTP_POST_VARS);
		extract($HTTP_GET_VARS);
	}
  	
	$BillNo = $BillNo; //订单号
	$Currency = $Currency; //币种
	$BankID = $BankID; //银行ID号
	$Amount = $Amount; //金额
	$Succeed = $Succeed; //支付状态
	$TradeNo = $TradeNo; //支付平台流水号
	$Result = $Result; //支付结果
	$MD5info = $MD5info; //取得的MD5校验信息
	$Remark = $Remark; //备注


	$MD5key = $this->_paymentInfo['config']['md5key']; //MD5私钥
	
	$md5src = $BillNo . $Currency . $Amount . $Succeed . $MD5key; //校验源字符串
	$md5sign = strtoupper(md5($md5src)); //MD5检验结果
	
  	$c = SiteModel::get_currency_info($Currency);
		if (isset($c['exchange']) && $c['exchange']) {
	   	$Amount = $Amount / $c['exchange'];
	 	}
	 	
	$result = array(
      'verified' => false,
      'paidAmount' => $Amount,
      'paymentId' => $TradeNo,
      'orderNumber' => $BillNo,
      'updateOrderStatus' => false,
      'redirect' => '',
      'message' => '',
    );

	/* 验证 */
	if ($MD5info == $md5sign) {

		if ($Succeed == '88' || $Succeed == '1' || $Succeed == '9' || $Succeed == '19') {
			$result['verified'] = true;
			$result['updateOrderStatus'] = true;
			$result['message'] = L('_PAYMENT_SUCCESS_');
		} else {
			$result['message'] =  L('_PAYMENT_FAILURE_');
		}
	} else {
		$result['message'] =  L('_PAYMENT_VALIDATION_FAILS_');
	}
	return $result;
  }
}